package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.bean.Userinfo;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @description: TOOD
 * @return:
 * @author: BaoZi
 * @date: 2022/9/20
 */
@RestController
@RequestMapping("/shopping")
public class CartController {

    @Autowired
    ICartService cartService;

    /**
     * 查看购物车
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author BaoZi
     * @since 2022/09/20 21:48
     */
    @GetMapping("/carts")
    public ResponseData getCartListById(HttpServletRequest request){
        String user_info = request.getHeader("user_info");
        Userinfo userinfo = JSON.parseObject(user_info, Userinfo.class);
        List<CartProductDto> cartListById = cartService.getCartListById(userinfo.getUid());
        return new ResponseUtil<>().setData(cartListById);
    }

    /**
     * 添加购物车
     * {"userId":"62","productId":100053202,"productNum":1}
     * @param cartRequest
     * @author BaoZi
     * @since 2022/09/20 21:46
     */
    @PostMapping("/carts")
    public ResponseData addToCart(@RequestBody CartRequest cartRequest){
        cartService.addToCart(cartRequest);
        return new ResponseUtil<>().setData("成功");
    }


    /**
     *更新购物车
     * {userId: "62", productId: 100053312, productNum: 3, checked: "true"}
     * @param cartRequest
     * @return void
     * @author BaoZi
     * @since 2022/09/20 21:49
     */
    @PutMapping("/carts")
    public ResponseData updateCartNum(@RequestBody CartRequest cartRequest){
        cartService.updateCartNum(cartRequest);
        return new ResponseUtil<>().setData("成功");
    }

}
